import 'package:dio/dio.dart';
import 'package:effective/model/result_entity.dart';
import 'package:effective/model/speech_entity.dart';
import 'package:effective/model/user_entity.dart';
import 'package:effective/utils/http/dio_util.dart';

class API {
  ///error是自定义的拦截器返回的错误类型，包含code与message两个参数，便于调试与异常处理
  ///抛出异常后会返回data为ResultEntity的Response

  Future<ResultEntity> register(UserEntity userEntity) async {
    Map data = await DioUtil.post(
      "/user/register",
      data: FormData.fromMap(userEntity.toJson()),
    ).catchError(
      (dioError) {
        var error = dioError.error;
        return Response(
          //没啥用，但是是必要参数
          requestOptions: RequestOptions(path: "/test"),
          data: ResultEntity.error(error.code, error.message).toJson(),
        );
      },
    );
    if (data == null) {
      return ResultEntity.error(-1, '服务器异常');
    }
    if (data['code'] != 0) {
      return ResultEntity().fromJson(data);
    } else {
      return ResultEntity.success(data['data']);
    }
  }

  Future<ResultEntity> login(UserEntity userEntity) async {
    Map data = await DioUtil.post(
      "/user/login",
      data: FormData.fromMap(userEntity.toJson()),
    ).catchError(
      (dioError) {
        var error = dioError.error;
        return Response(
          //没啥用，但是是必要参数
          requestOptions: RequestOptions(path: "/test"),
          data: ResultEntity.error(error.code, error.message).toJson(),
        );
      },
    );
    if (data == null) {
      return ResultEntity.error(-1, '服务器异常');
    }
    if (data['code'] != 0) {
      return ResultEntity().fromJson(data);
    } else {
      return ResultEntity.success(data['data']);
    }
  }

  Future<ResultEntity> logout(String token) async {
    Map data = await DioUtil.post("/user/logout",
        options: Options(headers: {'token': token})).catchError(
      (dioError) {
        var error = dioError.error;
        return Response(
          //没啥用，但是是必要参数
          requestOptions: RequestOptions(path: "/test"),
          data: ResultEntity.error(error.code, error.message).toJson(),
        );
      },
    );
    if (data == null) {
      return ResultEntity.error(-1, '服务器异常');
    }
    if (data['code'] != 0) {
      return ResultEntity().fromJson(data);
    } else {
      return ResultEntity.success(data['data']);
    }
  }

  Future<ResultEntity> createSpeech(
      String token, SpeechVideoEntity speechVideoEntity) async {
    print(speechVideoEntity.toJson());
    Map data = await DioUtil.get("/speech/createSpeech",
        params: speechVideoEntity.toJson(),
        options: Options(headers: {'token': token})).catchError(
      (dioError) {
        var error = dioError.error;
        return Response(
          //没啥用，但是是必要参数
          requestOptions: RequestOptions(path: "/test"),
          data: ResultEntity.error(error.code, error.message).toJson(),
        );
      },
    );
    if (data == null) {
      return ResultEntity.error(-1, '服务器异常');
    }
    if (data['code'] != 0) {
      return ResultEntity().fromJson(data);
    } else {
      return ResultEntity.success(data['data']);
    }
  }

  Future<ResultEntity> getSpeech(String token) async {
    Map data = await DioUtil.get("/speech/speech",
        options: Options(headers: {'token': token})).catchError(
      (dioError) {
        var error = dioError.error;
        return Response(
          //没啥用，但是是必要参数
          requestOptions: RequestOptions(path: "/test"),
          data: ResultEntity.error(error.code, error.message).toJson(),
        );
      },
    );
    if (data == null) {
      return ResultEntity.error(-1, '服务器异常');
    }
    if (data['code'] != 0) {
      return ResultEntity().fromJson(data);
    } else {
      print(data);
      return ResultEntity.success(data['data']);
    }
  }

  Future<ResultEntity> uploadVideo(String token, MultipartFile file) async {
    Response response = await new Dio().post("/speech/uploadVideo",
        data: FormData.fromMap({'video': file}),
        options: Options(headers: {'token': token})).catchError(
      (dioError) {
        var error = dioError.error;
        return Response(
          //没啥用，但是是必要参数
          requestOptions: RequestOptions(path: "/test"),
          data: ResultEntity.error(error.code, error.message).toJson(),
        );
      },
    );
    Map data = response.data;
    if (data == null) {
      return ResultEntity.error(-1, '服务器异常');
    }
    if (data['code'] != 0) {
      return ResultEntity().fromJson(data);
    } else {
      print(data);
      return ResultEntity.success(data['data']);
    }
  }

  Future<ResultEntity> updateSpeech(
      String token, SpeechVideoEntity speechVideoEntity) async {
    Map data = await DioUtil.post("/speech/updateSpeech",
        data: FormData.fromMap(speechVideoEntity.toJson()),
        options: Options(headers: {'token': token})).catchError(
      (dioError) {
        var error = dioError.error;
        return Response(
          //没啥用，但是是必要参数
          requestOptions: RequestOptions(path: "/test"),
          data: ResultEntity.error(error.code, error.message).toJson(),
        );
      },
    );
    if (data == null) {
      return ResultEntity.error(-1, '服务器异常');
    }
    if (data['code'] != 0) {
      return ResultEntity().fromJson(data);
    } else {
      return ResultEntity.success(data['data']);
    }
  }
}
